package com.css.fxfzypg.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * jdbc连接工具类
 */

public class JdbcUtil {

    @Value("${spring.datasource.url}")
    public static  String jdbcUrl;
    @Value("${spring.datasource.username}")
    public static  String username;
    @Value("${spring.datasource.password}")
    public static  String password;
    @Value("${spring.datasource.driver-class-name}")
    public static  String className;

    /**
     * jdbc连接数据库，执行drop create table SQL
     *
     * @param sql   sql语句
     */
//    public static void executeSql(String sql){
//        Connection conn = null;
//        PreparedStatement psmt = null;
//        boolean rs;
////        String jdbcUrl = "jdbc:postgresql://127.0.0.1:5432/fxfzypg";
//        String jdbcUrl = "jdbc:postgresql://10.7.209.233:5432/fxfzypg_country";
////        String jdbcUrl = "jdbc:postgresql://10.7.208.200:5432/fxfzypgcs";
//        try {
////            加载驱动
//            Class.forName("org.postgresql.Driver");
////            创建连接
////            conn = DriverManager.getConnection(jdbcUrl, "postgres", "123456");
//            conn = DriverManager.getConnection(jdbcUrl, "postgres", "Fxfz@13579#");
////            预编译sql
//            psmt = conn.prepareStatement(sql);
////            执行sql
//            rs = psmt.execute();
//            System.out.println(rs);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                psmt.close();
//                conn.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//    }
    public static void executeSql(String sql){
        Connection conn = null;
        PreparedStatement psmt = null;
        boolean rs;
        //String jdbcUrl = "jdbc:postgresql://10.7.209.233:5432/fxfzypg";
//        String jdbcUrl = "jdbc:postgresql://10.7.208.200:5432/fxfzypgcs";
        try {
//            加载驱动
            if(StringUtils.isBlank(className)){
                className = "org.postgresql.Driver";
            }
            Class.forName(className);
//            创建连接
            if(StringUtils.isBlank(jdbcUrl)){
                jdbcUrl = "jdbc:postgresql://10.7.209.233:5432/fxfzypg?stringtype=unspecified&useUnicode=true&characterEncoding=UTF-8&reWriteBatchedInserts=true";
            }
            if(StringUtils.isBlank(username)){
                username = "postgres";
            }
            if(StringUtils.isBlank(password)){
                password = "Fxfz@13579#";
            }
            conn = DriverManager.getConnection(jdbcUrl, username, password);
//            预编译sql
            psmt = conn.prepareStatement(sql);
//            执行sql
            rs = psmt.execute();
            System.out.println(rs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }





   }
